package com.aplink.generic.dialog;

import java.io.Serializable;

import android.app.ProgressDialog;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.type.ActionType;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.util.Language;

public class ApLinkProgressDialog implements Serializable {
	private static final long serialVersionUID = -3860989041628167720L;
	private boolean mCancelable;
	private boolean mCanceledOnTouchOutside;
	private final GenericDictionary<String, DialogInfo> mDialogInfos;
	private transient GenericFragment mGenericFragment;
	private transient ProgressDialog progressDialog;

	public ApLinkProgressDialog() {
		this.mDialogInfos = new GenericDictionary<String, DialogInfo>();
	}

	public void dismiss() {
		if (this.progressDialog != null) {
			if (this.progressDialog.isShowing()) {
				this.progressDialog.dismiss();
			}
		}
	}

	public GenericFragment getGenericFragment() {
		return this.mGenericFragment;
	}

	public ProgressDialog getProgressDialog(final String key) {
		final DialogInfo dialogInfo = this.mDialogInfos.get(key);
		if (dialogInfo.getText() != null) {
			this.progressDialog.setMessage(Language.getInstance().get(
			        dialogInfo.getText()));
		}
		if (dialogInfo.getTitle() != null) {
			this.progressDialog.setTitle(Language.getInstance().get(
			        dialogInfo.getTitle()));
		}
		return this.progressDialog;
	}

	public boolean has(final ActionType actionType) {
		return this.mDialogInfos.containsKey(actionType.getValue());
	}

	public void init(final GenericFragment genericFragment) {
		this.progressDialog = new ProgressDialog(genericFragment.getActivity());
		this.progressDialog.setCancelable(this.mCancelable);
		this.progressDialog
		        .setCanceledOnTouchOutside(this.mCanceledOnTouchOutside);
	}

	public boolean isCancelable() {
		return this.mCancelable;
	}

	public boolean isCanceledOnTouchOutside() {
		return this.mCanceledOnTouchOutside;
	}

	public void put(final String dialogKey, final DialogInfo dialogInfo) {
		this.mDialogInfos.put(dialogKey, dialogInfo);
	}

	public void setCancelable(final boolean cancelable) {
		this.mCancelable = cancelable;
	}

	public void setCanceledOnTouchOutside(final boolean canceledOnTouchOutside) {
		this.mCanceledOnTouchOutside = canceledOnTouchOutside;
	}
}
